package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.dto.InviteInfoDTO;
import com.yuelao.yuelao_backend.dto.InviteRankingDTO;
import com.yuelao.yuelao_backend.service.InviteService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/invite")
@Slf4j
public class InviteController {

    @Resource
    private InviteService inviteService;

    /**
     * 获取用户邀请信息
     * GET /invite/info
     * @return 邀请信息
     */
    @GetMapping("/info")
    public ApiResponse<InviteInfoDTO> getInviteInfo() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        InviteInfoDTO info = inviteService.getInviteInfo(currentUserId);
        return ApiResponse.ok(info);
    }

    /**
     * 生成邀请链接
     * POST /invite/generate
     * @return 邀请链接
     */
    @PostMapping("/generate")
    public ApiResponse<String> generateInviteLink() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        String inviteLink = inviteService.generateInviteLink(currentUserId);
        return ApiResponse.ok(inviteLink);
    }

    /**
     * 领取邀请奖励
     * POST /invite/claim
     * @param inviteRecordId 邀请记录ID
     * @return 是否成功
     */
    @PostMapping("/claim")
    public ApiResponse<Boolean> claimInviteReward(@RequestParam Long inviteRecordId) {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        boolean result = inviteService.claimInviteReward(currentUserId, inviteRecordId);
        return ApiResponse.ok(result);
    }

    /**
     * 获取邀请排行榜
     * GET /invite/ranking
     * @param page 页码
     * @param size 每页数量
     * @return 排行榜列表
     */
    @GetMapping("/ranking")
    public ApiResponse<List<InviteRankingDTO>> getInviteRanking(@RequestParam(defaultValue = "1") int page,
                                                                @RequestParam(defaultValue = "10") int size) {
        List<InviteRankingDTO> ranking = inviteService.getInviteRanking(page, size);
        return ApiResponse.ok(ranking);
    }

    /**
     * 获取邀请活动信息
     * GET /invite/events
     * @return 邀请活动信息列表
     */
    @GetMapping("/events")
    public ApiResponse<List<String>> getInviteEvents() {
        List<String> events = inviteService.getInviteEvents();
        return ApiResponse.ok(events);
    }
}